/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.service.plugin;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.portal.common.PortalPlugin;
import com.apache.portal.common.annotion.AnntationBean;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * description: org对接插件
 */
@AnntationBean(name = "org")
public class OrgSystemPluginImpl implements PortalPlugin {

    private Logger log = Logger.getLogger(OrgSystemPluginImpl.class);

    public Object doInvoke(HttpServletRequest request, HttpServletResponse response,
            Map<String, String> params) {
        String doCode = params.get("doCode");
        ResultMsg msg = null;
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.putAll(params);
        LoginUser loginUser = PortalPubFactory.getInstance().getLoginUser(request);
        if (loginUser != null) {
            paramsMap.put("userId", loginUser.getUserId());
        }
        if ("orgSave".equals(doCode)) {
            msg = orgSave(request, paramsMap);
        } else if ("orgInfo".equals(doCode)) {
            return orgInfo(request, paramsMap);
        } else if ("orgList".equals(doCode)) {
            if (StrUtil.isNotNull(params.get("orgAll"))) {
                paramsMap.put("orgAll", params.get("orgAll"));
            }
            paramsMap.put("pageIndex", params.get("pageIndex"));
            paramsMap.put("pageSize", params.get("pageSize"));
            paramsMap.put("userId", loginUser.getUserId());
            return orgList(request, paramsMap);
        } else if ("orgDel".equals(doCode)) {
            return orgDel(request, paramsMap);
        } else if ("deptSave".equals(doCode)) {
            paramsMap.put("nowUserEname", loginUser.getUserEname());
            msg = deptSave(request, paramsMap);
        } else if ("deptInfo".equals(doCode)) {
            return orgInfo(request, paramsMap);
        } else if ("deptList".equals(doCode)) {
            return deptList(request, paramsMap);
        } else if ("deptDel".equals(doCode)) {
            return deptDel(request, paramsMap);
        } else if ("roleSave".equals(doCode)) {
            paramsMap.put("nowUserEname", loginUser.getUserEname());
            params.put("sysFlag", loginUser.getSysFlag());
            if (!"1".equals(loginUser.getSysFlag())) {
                params.put("delStatus", loginUser.getOrgId());
            }
            msg = roleSave(request, paramsMap);
        } else if ("roleList".equals(doCode)) {
            return roleList(request, paramsMap);
        } else if ("roleDel".equals(doCode)) {
            paramsMap.put("method", "delete");
            return getRpcResult("roleAction", paramsMap);
        } else if ("orgActData".equals(doCode)) {
            paramsMap.put("method", doCode);//id,orgId,nowOrgId,nowSysUser
            paramsMap.put("nowOrgId", loginUser.getOrgId());
            paramsMap.put("nowSysUser", loginUser.getSysFlag());
            paramsMap.put("ParamType", "json");
            return getRpcResult("orgAction", paramsMap);
        } else if ("roleInfo".equalsIgnoreCase(doCode)) {
            paramsMap.put("method", "roleInfo");//id,orgId,nowOrgId,nowSysUser
            return getRpcResult("roleAction", paramsMap);
        } else if ("sysInfo".equalsIgnoreCase(doCode)) {
            //paramsMap.put("method", "sysEntity")//sysId
            paramsMap.put("propertyName", "sysId");
            paramsMap.put("propertyValue", params.get("sysId"));
            return getRpcResult("sysEntity", paramsMap);
        } else if ("saveSys".equalsIgnoreCase(doCode)) {
            paramsMap.put("method", "sysSave");//
            paramsMap.put("userEname", loginUser.getUserEname());
            return getRpcResult("sysEntity", paramsMap);
        } else if ("listSys".equalsIgnoreCase(doCode)) {
            paramsMap.put("method", "sysData");//
            return getRpcResult("sysEntity", paramsMap);
        }
        return msg;
    }

    private ResultMsg orgSave(HttpServletRequest request, Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");

        String sysEname = ConfigUtil.getInstance().getValueByKey("sys_ename");
        params.put("sysEname", sysEname);
        params.put("createTime",Long.toString(System.currentTimeMillis()));
        ResultEntity entity = getRpcResult("orgSave", params);
        if (("true".equals(entity.getResult()))) {
            if (!ToolsUtil.isEmpty(entity.getEntity())) {
                msg = new ResultMsg("T",String.valueOf(entity.getEntity()));

            } else {
                msg = new ResultMsg("F", entity.getMessage());
            }
        }
        return msg;
    }

    private Object orgInfo(HttpServletRequest request, Map<String, Object> params) {

        if (Validator.isNotNull(String.valueOf(params.get("orgId")))) {
            params.put("propertyName", "orgId");
            params.put("propertyValue", String.valueOf(params.get("orgId")));
        }
        if (Validator.isNotNull(String.valueOf(params.get("orgEname")))) {
            params.put("propertyName", "orgEname");
            params.put("propertyValue", String.valueOf(params.get("orgEname")));
        }
        ResultEntity entity = getRpcResult("orgEntity", params);
        return entity;
    }

    private Object orgList(HttpServletRequest request, Map<String, Object> params) {

        //params.put("orgAll", "orgAll");
        ResultEntity entity = (ResultEntity) getRpcResult("orgList", params);
        return entity;
    }

    private Object deptList(HttpServletRequest request, Map<String, Object> params) {
        if (Validator.isNotNull(request.getParameter("orgId"))) {
            params.put("orgId", request.getParameter("orgId"));
        }

        String fatherId = Validator.getDefaultStr(request.getParameter("fatherId"), "0");
        params.put("fatherId", fatherId);

        ResultEntity entity = (ResultEntity) getRpcResult("deptList", params);
        return entity;
    }

    private Object roleList(HttpServletRequest request, Map<String, Object> params) {
        LoginUser loginUser = PortalPubFactory.getInstance().getLoginUser(request);
        if (Validator.isNotNull(request.getParameter("orgId"))) {
            params.put("orgId", request.getParameter("orgId"));
        }
        params.put("pageSize", params.get("rows"));
        params.put("pageIndex", params.get("page"));
        params.put("sysUser", loginUser.getSysFlag());
        params.put("userOrgId", loginUser.getOrgId());
        ResultEntity entity = (ResultEntity) getRpcResult("roleList", params);
        return entity;
    }

    private ResultMsg orgDel(HttpServletRequest request, Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");
        params.put("infoType", "org");
        ResultEntity entity = getRpcResult("orgDel", params);
        if (("true".equals(entity.getResult()))) {
            if (!ToolsUtil.isEmpty(entity.getEntity())) {
                msg = new ResultMsg("T", entity.getMessage());

            } else {
                msg = new ResultMsg("F", entity.getMessage());
            }
        }
        return msg;
    }

    private ResultMsg deptSave(HttpServletRequest request, Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");
        String sysEname = ConfigUtil.getInstance().getValueByKey("sys_ename");
        params.put("sysEname", sysEname);
        ResultEntity entity = getRpcResult("deptAction", params);
        if (("true".equals(entity.getResult()))) {
            if (!ToolsUtil.isEmpty(entity.getEntity())) {
                msg = new ResultMsg("T", entity.getMessage());

            } else {
                msg = new ResultMsg("F", entity.getMessage());
            }
        }
        return msg;
    }

    private ResultMsg deptDel(HttpServletRequest request, Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");
        String sysEname = ConfigUtil.getInstance().getValueByKey("sys_ename");
        params.put("sysEname", sysEname);
        ResultEntity entity = getRpcResult("deptAction", params);
        if (("true".equals(entity.getResult()))) {
            if (!ToolsUtil.isEmpty(entity.getEntity())) {
                msg = new ResultMsg("T", entity.getMessage());

            } else {
                msg = new ResultMsg("F", entity.getMessage());
            }
        }
        return msg;
    }

    private ResultMsg roleSave(HttpServletRequest request, Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");
        String sysEname = ConfigUtil.getInstance().getValueByKey("sys_ename");
        params.put("sysEname", sysEname);
        params.put("method", "save");
        ResultEntity entity = getRpcResult("roleAction", params);
        if (("true".equals(entity.getResult()))) {
            if (!ToolsUtil.isEmpty(entity.getEntity())) {
                msg = new ResultMsg("T", "保存成功");
            } else {
                msg = new ResultMsg("F", entity.getMessage());
            }
        }
        return msg;
    }

    private ResultEntity getRpcResult(String methodKey, Map<String, Object> params) {
        InterfaceRegister ss = PortalPubFactory.getInstance().getRpcInfo("uct");
        return LoadRpcService.service().doServiceClient("uctService", methodKey, params, ss);
    }

}
